package cn.source.system.service.impl;

import java.io.File;
import java.util.List;

import cn.source.common.utils.DateUtils;
import cn.source.system.domain.RecoveryTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import cn.source.system.mapper.DbRecordMapper;
import cn.source.system.domain.DbRecord;
import cn.source.system.service.IDbRecordService;

import static cn.source.common.utils.file.FileUtils.deleteFolder;

/**
 * 数据备份记录Service业务层处理
 *
 * @author sourcebyte.vip
 * @date 2025-02-21
 */
@Service
public class DbRecordServiceImpl implements IDbRecordService {
    @Autowired
    private DbRecordMapper dbRecordMapper;

    @Value("${backup.path}")
    private String backupPath;

    /**
     * 查询数据备份记录
     *
     * @param id 数据备份记录主键
     * @return 数据备份记录
     */
    @Override
    public DbRecord selectDbRecordById(Long id) {
        return dbRecordMapper.selectDbRecordById(id);
    }

    /**
     * 查询数据备份记录列表
     * 
     * @param dbRecord 数据备份记录
     * @return 数据备份记录
     */
    @Override
    public List<DbRecord> selectDbRecordList(DbRecord dbRecord)
    {
        return dbRecordMapper.selectDbRecordList(dbRecord);
    }

    /**
     * 新增数据备份记录
     * 
     * @param dbRecord 数据备份记录
     * @return 结果
     */
    @Override
    public int insertDbRecord(DbRecord dbRecord)
    {
        return dbRecordMapper.insertDbRecord(dbRecord);
    }

    /**
     * 修改数据备份记录
     * 
     * @param dbRecord 数据备份记录
     * @return 结果
     */
    @Override
    public int updateDbRecord(DbRecord dbRecord)
    {
        return dbRecordMapper.updateDbRecord(dbRecord);
    }

    /**
     * 批量删除数据备份记录
     * 
     * @param ids 需要删除的数据备份记录主键
     * @return 结果
     */
    @Override
    public int deleteDbRecordByIds(Long[] ids)
    {
        return dbRecordMapper.deleteDbRecordByIds(ids);
    }

    /**
     * 删除数据备份记录信息
     *
     * @param id 数据备份记录主键
     * @return 结果
     */
    @Override
    public int deleteDbRecordById(Long id) {
        DbRecord dbRecord = dbRecordMapper.selectDbRecordById(id);
        String s = DateUtils.dateTimeSecond(dbRecord.getStartTime());
        String name = dbRecord.getTaskName();//任务名称
        String rootPath = backupPath + File.separator + name;
        File file = new File(rootPath, s);
        if (file.exists()) {
            deleteFolder(file);
        }
        return dbRecordMapper.deleteDbRecordById(id);
    }

    @Override
    public List<DbRecord> selectDbRecordForTimeLimit(Integer maxNum,String taskName) {
        return dbRecordMapper.selectDbRecordForTimeLimit(maxNum,taskName);
    }

//    List<RecoveryTable>

}
